
/**
 * 加入购物车
 */
function buy(goodsid){
	$.post("/order/buy", {goodsid:goodsid}, function(data){
        console.log(data);
        let obj = JSON.parse(data);
		if(obj.state==="ok"){
			layer.msg("购买成功!", {time:800}, function(){
                updateAfterBuy(obj.total);
			});
		}else if(obj.state==="login"){
			alert("请登录后购买!");
			location.href="/user/login";
		}else if(obj.state==="empty"){
			alert("库存不足!");
			location.reload();
		}else{
			alert("请求失败!");
		}
	});
}
/**
 * 购物车减去
 */
function lessen(goodsid){
	$.post("/order/reduce", {goodsid:goodsid}, function(data){
        let obj = JSON.parse(data);
		if(obj.state==="ok"){
			layer.msg("操作成功!", {time:800}, function(){
                updateAfterBuy(obj.total);
			});
		}else if(obj.state==="login"){
			alert("请登录后操作!");
			location.href="/user/login";
		}else{
			alert("请求失败!");
		}
	});
}
/**
 * 购物车删除
 */
function deletes(goodsid){
	$.post("/order/delete", {goodsid:goodsid}, function(data){
        let obj = JSON.parse(data);
		if(obj.state==="ok"){
			layer.msg("删除成功!", {time:800}, function(){
                updateAfterBuy(obj.total);
			});
		}else if(obj.state==="login"){
			alert("请登录后操作!");
			location.href="login.jsp";
		}else{
			alert("请求失败!");
		}
	});
}

function updateAfterBuy(total) {
    if (document.getElementById("cart_sym")) {
        location.reload();
    } else {
        $("#cart_num").text(total);
    }
}